package com.budgetme.ui.activities;

import java.util.ArrayList;

import com.budgetme.R;
import com.budgetme.database.Database;
import com.budgetme.ui.fragments.BudgetListFragment;
import com.budgetme.ui.fragments.ExpenseListFragment;
import com.budgetme.ui.fragments.IncomeListFragment;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;


public class Overview extends AbstractActivity {
	private static final String TAG = Overview.class.getName();
	
	ViewPager mViewPager;
	TabsAdapter mTabsAdapter;

	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
		
        setContentView(R.layout.overview);
        mViewPager = (ViewPager) findViewById(R.id.overview_pager);

        final ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

        mTabsAdapter = new TabsAdapter(this, mViewPager);
        
        ActionBar.Tab overviewTab = bar.newTab().setText("Overview");
        Bundle overviewArgs = new Bundle();
        overviewArgs.putInt(BudgetListFragment.ARG_FILTER, Database.tbl_budgets.FILTER_ENABLED);
        
        ActionBar.Tab expensesTab = bar.newTab().setText("Expenses");
        
        
        ActionBar.Tab incomeTab = bar.newTab().setText("Income");
        
        mTabsAdapter.addTab(incomeTab, IncomeListFragment.class, null);
        mTabsAdapter.addTab(overviewTab, BudgetListFragment.class, overviewArgs);
        mTabsAdapter.addTab(expensesTab, ExpenseListFragment.class, null);
        
        mTabsAdapter.onTabSelected(overviewTab, null);
        
        
		
	}
	
    public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {

        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
    	
        public TabsAdapter(Activity activity, ViewPager pager) {
            super(activity.getFragmentManager());
            mContext = activity;
            mActionBar = activity.getActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }
        
        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            
            // Set the tag for the tab so that we can find it's position later
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            
            // Add the tab to the action bar
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }


		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			mActionBar.setSelectedNavigationItem(position);
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Object tag = tab.getTag();
            for (int i=0; i<mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {			
		}

		@Override
		public Fragment getItem(int position) {
			TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
		}

		@Override
		public int getCount() {
			return mTabs.size();
		}
    	
        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(Class<?> _class, Bundle _args) {
                clss = _class;
                args = _args;
            }
        }
		
    }
	
	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
	}
	
	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}

	
}
